package com.tanjor.main.sys.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tanjor.common.datatable.PageReq;
import com.tanjor.common.datatable.PageResp;
import com.tanjor.common.util.ConstantUtil;
import com.tanjor.sys.dubbo.dto.SysMenuDTO;
import com.tanjor.sys.dubbo.dto.SysRoleDTO;
import com.tanjor.sys.dubbo.impl.interfaces.ISysMenuDubbo;

@Controller
@RequestMapping("/sys/menu")
public class MenuController {

	@Resource
	private ISysMenuDubbo sysMenuDubbo;
	
	@RequestMapping("/list")
	public String list(Model model){
		return "manage/sys/menu-list";
	}
	
	@RequestMapping("/loadPage")
	@ResponseBody
	public PageResp loadPage(PageReq pageReq){
		PageResp page = sysMenuDubbo.findPage(pageReq);
		return page;
	}
	
	@RequestMapping("/goEdit/{id}")
	public String eidt(Model model,@PathVariable String id){
		model.addAttribute("id", id);
		return "manage/sys/menu-edit";
	}
	
	@RequestMapping("/goAdd")
	public String add(){
		return "manage/sys/menu-edit";
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public Map<String,Object> save(SysMenuDTO menu){
		if(StringUtils.isBlank(menu.getPId())){
			menu.setPId("0");
		}
		if(StringUtils.isNotBlank(menu.getId())){
			return sysMenuDubbo.update(menu);
		}else{
			return sysMenuDubbo.insert(menu);
		}
	}
	
	@RequestMapping("/del")
	@ResponseBody
	public Map<String,Object> del(SysMenuDTO menu){		
		 try{
			 return sysMenuDubbo.deleteById(menu.getId());
		 }catch (Exception e) {
			 Map<String,Object> result = new HashMap<String, Object>();
			// TODO: handle exception
			 result.put("result", false);
			 result.put("msg", "删除失败！");
			 return result;		
		 }	
	}
	
	@RequestMapping("/getModel")
	@ResponseBody
	public SysMenuDTO getModel(String id){
		if(StringUtils.isNotBlank(id)){
			SysMenuDTO menu = sysMenuDubbo.findById(id);
			return menu;
		}else{
			return new SysMenuDTO();
		}
	}
	
	@RequestMapping("/loadAllMenu")
	@ResponseBody
	public List<SysMenuDTO> loadAllMenus(){
		List<SysMenuDTO> menus = sysMenuDubbo.findAllMenus();
		for(int i=0;i<menus.size();i++){
			menus.get(i).setUrl("");
		}
		return menus;
	}
	
	@RequestMapping("/loadTreeMenu")
	@ResponseBody
	public List<Map<String,Object>> loadTreeMenu(String cacheKey){
		List<Map<String,Object>> sysmenus = sysMenuDubbo.findTreeByRole("sys_role_admin");		
		return sysmenus;
	}	
	
	@RequestMapping("/loadTopTreeMenu")
	@ResponseBody
	public List<Map<String,Object>> loadTopTreeMenu(String cacheKey){
		List<Map<String,Object>> sysmenus = sysMenuDubbo.findTopTree();		
		return sysmenus;
	}
	
	@RequestMapping("/loadMenu")
	@ResponseBody
	public List<Map<String,Object>> loadMenu(HttpServletRequest request){		
		HttpSession session = request.getSession();		
		SysRoleDTO sysRoleDTO = (SysRoleDTO) session.getAttribute(ConstantUtil.SESSION.SYS_SESSION_ROLE);
		List<Map<String,Object>> sysmenus = (List<Map<String,Object>>)session.getAttribute(ConstantUtil.SESSION.SYS_SESSION_ROLE_MENU);
		if(sysRoleDTO!=null){
			String roleId = sysRoleDTO.getCode();			
			if(sysmenus==null||sysmenus.size()==0){
				sysmenus = sysMenuDubbo.findByRole(roleId);
				session.setAttribute(ConstantUtil.SESSION.SYS_SESSION_ROLE_MENU, sysmenus);
			}
		}
		return sysmenus;
	}
}
